package core

import (
	"context"
	"fastgin/global"

	"github.com/redis/go-redis/v9"
	"github.com/sirupsen/logrus"
)

func InitRedis() (client *redis.Client) {
	cfg := global.Config.Redis
	if cfg.Status {
		client = redis.NewClient(&redis.Options{
			Addr:     cfg.Addr,
			Password: cfg.Passwd,
			DB:       cfg.DB,
		})
		_, err := client.Ping(context.Background()).Result()
		if err != nil {
			logrus.Errorf("连接redis失败 %s", err)
			return nil
		}
		logrus.Infof("连接redis成功 %s", cfg.Addr)
		return client
	}
	logrus.Warnf("Redis组件未启用,跳过Redis连接初始化")
	return nil
}
